package cn.jsj.hzu.qm.facecloud.ciuser.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.hzu.jsj.utils.BeanFactory;
import cn.jsj.hzu.qm.facecloud.ciuser.pojo.Cilab;
import cn.jsj.hzu.qm.facecloud.ciuser.pojo.Ciuser;
import cn.jsj.hzu.qm.facecloud.ciuser.service.CilabService;

/**
 * Servlet implementation class CilabServlet
 */
public class CilabServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;encoding=UTF-8");

		PrintWriter out = response.getWriter();

		CilabService cilabService = BeanFactory.getBean("cilabService");

		String modeString = request.getParameter("mode");
		if (modeString == null) {
			response.getWriter().write("no mode");
			return;
		}
		
		
		//针对未登录用户的非法访问
		Ciuser ciuser = (Ciuser) request.getSession().getAttribute("ciuser");
		if (ciuser==null|| ciuser.getType() == 0) {
			response.sendError(566, "没有登录");
			return;
		}

		int mode;
		mode = Integer.parseInt(modeString);

		switch (mode) {
		// 增
		case 11: {
			System.out.println("11 增");
			String nameString = request.getParameter("name");
			String locationString = request.getParameter("location");
			String memoString = request.getParameter("memo");

			// 参数缺失
			if (nameString == null || locationString == null || memoString == null) {
				out.write("{'state':'param lost'}");
				return;
			}

			Cilab cilab = new Cilab();
			cilab.setName(nameString);
			cilab.setLocation(locationString);
			cilab.setMemo(memoString);

			int i = cilabService.addCilab(cilab);

			// 返回：受影响的行数
//			out.write("{'state':'" + i + "'}");
			if(i != 0) {
				out.write("<script>alert('添加成功！'); window.location.href='checkin/cilabregister.html'</script>");
			}
			else {
				out.write("<script>alert('添加失败：i = "+i+"'); window.location.href='checkin/cilabregister.html'</script>");
			}
			return;
		}

		// 改
		case 31: {
			String idString = request.getParameter("id");
			String nameString = request.getParameter("name");
			String locationString = request.getParameter("location");
			String memoString = request.getParameter("memo");
			String availableString = request.getParameter("available");

			// 参数缺失
			if (idString == null ||  nameString == null || locationString == null || memoString == null||availableString== null) {
				out.write("{'state':'param lost'}");
				return;
			}
			
			// 设置
			Cilab cilab = new Cilab();
			
			int id = Integer.parseInt(idString);
			cilab.setId(id);
			
			cilab.setName(nameString);
			cilab.setLocation(locationString);
			cilab.setMemo(memoString);
			
			int available = Integer.parseInt(availableString);
			cilab.setAvailable(available);
			

			int i = cilabService.modifyLab(cilab);

			// 返回：受影响的行数
//			out.write("{'state':'" + i + "'}");
			
			//转回原始的修改的界面
			out.write("<script>alert('修改成功');window.location.href='checkin/jumpToServlet?mode=cilab&id="+cilab.getId()+"'</script>");
			return;
		}

		
		// 查
		
		//查询所有的实验室
		case 41: {
			
			//switch ciuser.getType()
			List<Cilab> cilabs;
			switch (ciuser.getType()) {
			case 1:{
				cilabs = cilabService.getAllCilabs();
				break;
			}
			case 2:{
				cilabs = new LinkedList<Cilab>();
				String myCilabNameString = ciuser.getMemo();
				cilabs.add(cilabService.findCilabByName(myCilabNameString));
				break;
				
			}
			default:{
				cilabs = new LinkedList<Cilab>();
				break;
			}
			}//end switch ciuser.getType()
			
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append('[');
			for(Cilab myCilab : cilabs ) {
				stringBuilder.append(myCilab.toJsonString());
				stringBuilder.append(',');
			}
			stringBuilder.append(']');
			
			out.write(stringBuilder.toString());
			
			return;
		}
		
		//查询实验室是否存存在
		case 42:{
			String nameString = request.getParameter("name");
			if (nameString == null) {
				out.write("{'state':'param lost'}");
				return;
			}
			
			Cilab cilab = cilabService.findCilabByName(nameString);
			
			if (cilab != null) {
				out.write("{'state':'existed'}");
				System.out.println("{'state':'existed'}");
				return;
			}else {
				out.write("{'state':'notfound'}");
				System.out.println("{'state':'notfound'}");
				return;
			}
		
		}
		
		//查询所有实验室的名字
		case 43:{
			
			// 利用CiLabName中的接口，单独查询实验室名字的列表，
			// 并将其转换为json格式的纯字符串返回
			List<String> cilabNameStrings = cilabService.getAllCilabName();
			
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append('[');
			for(String cilabName : cilabNameStrings) {
				stringBuilder.append(cilabName);
				stringBuilder.append(',');
			}
			stringBuilder.append(']');
			
			out.write(stringBuilder.toString());
		}
		
		
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
